package com.ajax;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.model.DataBase;

/**
 * Servlet implementation class StoreInfo
 */
@WebServlet("/files/StoreInfo")
public class StoreInfo extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public StoreInfo() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		try {
			Connection con = DataBase.getConnection();
			PreparedStatement pstmtRoom = con.prepareStatement("SELECT room FROM store_room WHERE name=?");
			pstmtRoom.setString(1, request.getParameter("slt"));
			ResultSet rsRoom = pstmtRoom.executeQuery();
			PrintWriter out = response.getWriter();
			if( rsRoom.next() )
				out.println("{\"room\":\""+rsRoom.getString("room")+"\"}");
			else
				out.println("{\"room\":\""+"Wrong Ajax Request"+"\"}");
			rsRoom.close();
			con.close();
		} catch (SQLException e2) {
			e2.printStackTrace();
		}
	}

}
